package com.wing.auth.controller;

import com.wing.auth.entity.response.GetTokenResponse;
import com.wing.auth.entity.response.TicketSendResponse;
import com.wing.auth.service.AuthService;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.redis.utils.redis.RedisHelperImpl;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 畅捷通-应用授权2
 *
 * @author xiayang
 */
@Validated
@Api(value = "畅捷通-应用授权", tags = "#/chanjet/auth")
@RestController("chanjetAuthController")
@RequestMapping("auth/admin/auth")
public class AuthController {

    @Resource
    AuthService authService;

    @Resource
    RedisHelperImpl redisHelper;

    /**
     * 立即触发appTicket推送
     */
    @PostMapping("/appTicketResend")
    public TicketSendResponse appTicketResend() {
        return authService.appTicketSend();
    }

    /**
     * 自建应用获取token
     */
    @PostMapping("/generateToken")
    public JsonResult generateToken() {
        try {
            return JsonResult.success(authService.getToken());
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 获取对应的appTicket信息
     * @return
     */
    @GetMapping("/getAppTicketInfo")
    public JsonResult getAppTicketInfo() {
        try {
            if(redisHelper.getValue("appTicket")!=null){
                return JsonResult.success(redisHelper.getValue("appTicket").toString());
            }
            return JsonResult.fail();
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
    }


    /**
     * 设置 redis APP_TICKET 缓存
     * @return
     */
    @GetMapping("/setAppTicketInfo")
    public JsonResult setAppTicketInfo(@RequestParam("appTicket") String appTicket) {
        try {

            redisHelper.valuePut("appTicket", appTicket);

            return JsonResult.success();
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
    }



}
